package screen.component
{
	import com.adobe.serialization.json.JSON;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	
	import helper.Resources;
	import helper.Services;
	import helper.ValueObject;
	
	import screen.TechComBankScreen;

	public class MainBlock extends UI
	{
		private const _UNIT_NAME:String ='u';
		private const _MILLION:String =" Triệu"
		private var _block:MovieClip;
		private var _no:Number;
		private var _gift_buy:MovieClip;
		private var _bonus:MovieClip;
		private var _gift_buy_arr:Array=[];
		private var _buy_gift_bl:Boolean = false;
		public function MainBlock(no:Number)
		{
			_no = no;
			init();
		}
		private function init():void{
			_block = ValueObject.T1_RESOURCE.getExportedAsset("mainBlock") as MovieClip;
			addChild(_block);
		}
		public function updateGifts(arr:Array):void{
			_gift_buy_arr =[];
			_buy_gift_bl = false;
			var leng:int = arr.length;
			for(var i:int=0;i<leng;i++){
				var unit:MovieClip = _block.getChildByName(_UNIT_NAME+i) as MovieClip;
				unit.loader.load(new URLRequest(Resources.GIFTS_FOLDER + arr[i].giftId + ".png"));
				TextField(unit.value).text = arr[i].price + _MILLION;
				buttonImageInit(unit);
				unit.addEventListener(MouseEvent.CLICK,updateScore);
			}
			_bonus = _block.getChildByName("bonus") as MovieClip;
			_bonus.gotoAndStop(1);
		}
		public function updateATMCard(value:Number):void{
			var unit:MovieClip = _block.getChildByName(_UNIT_NAME+9) as MovieClip;
			unit.loader.load(new URLRequest(Resources.GIFTS_FOLDER + "1000.png"));
			unit.gotoAndStop("atm");
			TextField(unit.value).text = value.toString()  + _MILLION;
		}
		private function updateScore(e:MouseEvent):void{
			if(_gift_buy){
				return;
			}
			_gift_buy  = e.currentTarget as MovieClip;
			var atm_card:MovieClip = _block.getChildByName(_UNIT_NAME+9) as MovieClip;
			if(getValue(_gift_buy)>getValue(atm_card)){
				_gift_buy = null;
				return;
			}
			
			var value:Number = getValue(atm_card) - getValue(_gift_buy)
			TextField(atm_card.value).text = value.toString() + _MILLION;
			_bonus.gotoAndStop(1);
			TextField(_bonus.mcGP.txtNumber).text = "-"+getValue(_gift_buy);
			_bonus.play();
			buttonImageDisable(_gift_buy);
			_gift_buy.removeEventListener(MouseEvent.CLICK,updateScore);
			_gift_buy_arr.push(getValue(_gift_buy));
			_gift_buy = null;
			if(value ==0){
				var variable:String="";
				atm_card.gotoAndStop("over");
				var leng:Number = _gift_buy_arr.length;
				for(var i:Number=0;i<leng;i++){
					variable +=_gift_buy_arr[i];
					if(i<(leng -1)){
						variable +="-";
					}
				}
				switch(_no){
					case 0:
						variable ="/1/" + variable;
						break;
					case 1:
						variable ="/2/" + variable;
						break;
					case 2:
						variable ="/3/" + variable;
						break;
					case 3:
						variable ="/4/" + variable;
						break;
				}
				//trace("variable:",variable);
				Services.getInstance().updateScore(updateScoreComplete,updateScoreError,variable);
			}
			
			
		}
		private function getValue(unit:MovieClip):Number{
			var leng:Number = TextField(unit.value).text.length;
			return parseInt(TextField(unit.value).text.substring(0,leng - _MILLION.length));
		}
		private function updateScoreComplete(e:Event):void{
			URLLoader(e.target).removeEventListener(Event.COMPLETE,updateScoreComplete);
			URLLoader(e.target).removeEventListener(IOErrorEvent.IO_ERROR,updateScoreError);
			var atm_card:MovieClip = _block.getChildByName(_UNIT_NAME+9) as MovieClip;
			trace("e.target.data:",e.target.data);
			var result:Number = JSON.decode(e.target.data);
			switch(result){
				case 0:
					break;
				default:
					TechComBankScreen.getInstance().addScore(1);
					_buy_gift_bl = true;
					break;
			}
		}
		private function updateScoreError(e:IOErrorEvent):void{
			URLLoader(e.target).removeEventListener(Event.COMPLETE,updateScoreComplete);
			URLLoader(e.target).removeEventListener(IOErrorEvent.IO_ERROR,updateScoreError);
			_gift_buy = null;
		}
		public function get buyGift():Boolean{
			return _buy_gift_bl;
		}

	}
}